package com.xuan.book.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result<T> {

    private Integer code;   // 后端响应状态，业务状态码: 200-成功   -1 -失败  -2 -未登录
    private String errMsg;  // 后端发生错误的原因

    private T data;

    /**
     *   成功时
     */
    public static <T> Result<T>  success(T data) {
        return new Result<>(200, "", data);
    }

    /**
     *  失败时
     */
    public static <T> Result<T> fail(String errMsg) {
        return new Result<>(-1, errMsg, null);
    }
    public static <T> Result<T> fail(T data, String errMsg) {
        return new Result<>(-1, errMsg, data);
    }

    /**
     *  用户未登录
     */
    public static <T> Result<T> unLogin() {
        return new Result<>(-2, "用户未登录", null);
    }
}
